package com.example.lib_net.model.api

import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.example.lib_net.R

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/14
 */
//"records": [
//      {
//        "postsCommentId": "1888619289417928706",
//        "postsId": "1888206726502735874",
//        "firstCommentId": "0",
//        "parentId": "0",
//        "likeNumber": 1,
//        "level": null,
//        "content": "恭喜老哥实现愿望！！！",
//        "twoLevel": [],
//        "createName": "摩友259e60",
//        "headImg": null,
//        "beReplyId": "1887398656892264450",
//        "replyName": null,
//        "ifAuthor": 0,
//        "oneself": 0,
//        "createBy": "1806356792584241154",
//        "createTime": "2025-02-10 00:01:42",
//        "parentContent": null,
//        "ifSensitive": 0,
//        "isLike": 0,
//        "modelName": null,
//        "modelNickname": null,
//        "channel": 2,
//        "pinned": 0,
//        "parentChannel": null,
//        "commentNumber": 3
//      },
//      {
//        "postsCommentId": "1888821567466819586",
//        "postsId": "1888206726502735874",
//        "firstCommentId": "0",
//        "parentId": "0",
//        "likeNumber": 0,
//        "level": null,
//        "content": "我27岁也是这么说的，再不骑就没机会骑了",
//        "twoLevel": [],
//        "createName": "摩友2fbcad",
//        "headImg": null,
//        "beReplyId": "1887398656892264450",
//        "replyName": null,
//        "ifAuthor": 0,
//        "oneself": 0,
//        "createBy": "1868367019787628546",
//        "createTime": "2025-02-10 13:25:29",
//        "parentContent": null,
//        "ifSensitive": 0,
//        "isLike": 0,
//        "modelName": null,
//        "modelNickname": null,
//        "channel": 2,
//        "pinned": 0,
//        "parentChannel": null,
//        "commentNumber": 3
//      },
//      {
//        "postsCommentId": "1889127085019402241",
//        "postsId": "1888206726502735874",
//        "firstCommentId": "0",
//        "parentId": "0",
//        "likeNumber": 0,
//        "level": null,
//        "content": "勇敢的人先享受世界",
//        "twoLevel": [],
//        "createName": "摩友8b6095",
//        "headImg": null,
//        "beReplyId": "1887398656892264450",
//        "replyName": null,
//        "ifAuthor": 0,
//        "oneself": 0,
//        "createBy": "1791684378025492482",
//        "createTime": "2025-02-11 09:39:30",
//        "parentContent": null,
//        "ifSensitive": 0,
//        "isLike": 0,
//        "modelName": null,
//        "modelNickname": null,
//        "channel": 2,
//        "pinned": 0,
//        "parentChannel": null,
//        "commentNumber": 3
//      }
//    ],
//    "total": 3,
//    "size": 10,
//    "current": 1,
//    "orders": [],
//    "optimizeCountSql": true,
//    "searchCount": true,
//    "countId": null,
//    "maxLimit": null,
//    "pages": 1
data class PostCommentModel(
    val records: MutableList<PostCommentItem>,
    val total: Int,
    val size: Int,
    val current: Int,
    val orders: List<Any>,
    val optimizeCountSql: Boolean,
    val searchCount: Boolean,
    val countId: Any,
    val maxLimit: Any,
    val pages: Int
)
//........信息不全
data class PostCommentItem(
    val postsCommentId: String,
    val postsId: String,
    val firstCommentId: String,
    val parentId: String,
    val likeNumber: Int,
    val level: Any?,
    val content: String,
    val twoLevel: List<PostCommentItem>?, //二级评论
    val createName: String,
    val headImg: ImgItem?,
    val beReplyId: String,
    val replyName: String?,
    val ifAuthor: Int,
    val oneself: Int,
    val createBy: String,
    val createTime: String,
    val parentContent: Any?,
    val ifSensitive: Int,
    val isLike: Int,
    val modelName: Any?,
    val modelNickname: Any?,
    val channel: Int,
    val pinned: Int,
    val parentChannel: Any?,
    val commentNumber: Int,

    //额外字段
    var isFirstLevel:Boolean, //是否为一级评论
    var twoLevelSize: Int = 0, //下方是否有展开更多
){
    private fun isParentOfficial() = parentChannel == 1 //父评论是否为官方
    //回复Xxx
    fun requireReplay(): String {
        return if (isParentOfficial()) {
            "宗申骑士俱乐部官方"
        } else {
            replyName ?: "未知用户"
        }
    }
    // xxx回复xxx
    fun formatReplayContent(content: String, replyName: String?, context: Context): CharSequence {
        val prefix = String.format("%s", replyName ?: "未知用户")
        val formatContent = String.format("回复%s：%s", prefix, content)
        val start = formatContent.indexOf(prefix)
        val end = start + prefix.length
        val spannableStringBuilder = SpannableStringBuilder(formatContent)
        spannableStringBuilder.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_919191)),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return spannableStringBuilder
    }
    fun isPin(): Boolean = pinned == 1

    fun isOfficial() = channel == 1

    fun requireCreateName():String{
        return if (isOfficial()) {
            "宗申骑士俱乐部官方"
        } else {
            createName ?: "未知用户"
        }
    }

    fun requireAvatarUrl():String?{
        return if (isOfficial()) {
            null
        } else {
            headImg?.url
        }
    }
}
